package com.bsj.travel.security.auth;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bsj.travel.def.common.DO.ProductDO;
import com.bsj.travel.def.common.DO.UserDO;
import com.bsj.travel.def.common.DO.UserProductDO;
import com.bsj.travel.def.global.JsonResultEnum;
import com.bsj.travel.exception.ApiException;
import com.bsj.travel.mapper.mysql.ProductMapper;
import com.bsj.travel.mapper.mysql.UserMapper;
import com.bsj.travel.mapper.mysql.UserProductMapper;
import com.bsj.travel.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/***
 * @description AuthProduct
 * @author LiJiaXin
 * @version 1.0.0
 * @time 2024/1/20 9:54
 **/
@Slf4j
@Component
public class AuthProduct {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private UserProductMapper userProductMapper;

    @Resource
    private UserMapper userMapper;

    /**
     * 权限校验
     *
     * @param productId
     * @return void
     * @author ljx
     * @time 2024/1/17 16:09
     */
    public void permissionCheck(Long productId) {
        QueryWrapper<UserProductDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", SecurityUtils.getUserId());
        queryWrapper.eq("productId", productId);
        if (!userProductMapper.exists(queryWrapper)) {
            throw new ApiException(JsonResultEnum.NOT_AUTH);
        }
    }

    /**
     * 权限校验
     *
     * @param productKey
     * @return void
     * @author ljx
     * @time 2024/2/18 15:15
     */
    public void permissionCheck(String productKey) {
        ProductDO productDO = productMapper.selectOne(Wrappers.<ProductDO>lambdaQuery().eq(ProductDO::getProductKey, productKey));
        if (productDO == null) {
            log.info("产品不存在，产品KEY{}", productKey);
            throw new ApiException(JsonResultEnum.PRODUCT_IS_NOT_EXIST);
        }
        QueryWrapper<UserProductDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", SecurityUtils.getUserId());
        queryWrapper.eq("productId", productDO.getProductId());
        if (!userProductMapper.exists(queryWrapper)) {
            throw new ApiException(JsonResultEnum.NOT_AUTH);
        }
    }

    /**
     * 权限校验
     *
     * @param appId
     * @param productKey
     * @return
     * @author ljx
     * @time 2024/4/8 9:41
     */
    public String permissionCheck(String appId, String productKey) {
        UserDO userDO = userMapper.selectOne(Wrappers.<UserDO>lambdaQuery().eq(UserDO::getAppId, appId));
        if (userDO == null) {
            log.info("用户不存在，appId{}", appId);
            throw new ApiException(JsonResultEnum.NOT_ERR_OPEN_AUTH);
        }
        ProductDO productDO = productMapper.selectOne(Wrappers.<ProductDO>lambdaQuery().eq(ProductDO::getProductKey, productKey));
        if (productDO == null) {
            log.info("产品不存在，产品KEY{}", productKey);
            throw new ApiException(JsonResultEnum.NOT_ERR_OPEN_AUTH);
        }
        QueryWrapper<UserProductDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", userDO.getUserId());
        queryWrapper.eq("productId", productDO.getProductId());
        if (!userProductMapper.exists(queryWrapper)) {
            log.info("该产品不属于该用户");
            throw new ApiException(JsonResultEnum.NOT_ERR_OPEN_AUTH);
        }
        return userDO.getSecretKey();
    }
}
